package com.simple.common.framework.retrofit;

import com.simple.common.framework.RetrofitLoadCondition;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.net.ssl.X509TrustManager;
import java.util.concurrent.TimeUnit;

/**
 * @author yasud
 * @date 2021年03月02日 17:09
 * @description
 */
@Configuration
@Conditional({RetrofitLoadCondition.class})
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public OkHttpClient okHttpClient() {
        X509TrustManager trustManager = SSLFactoryUtil.getX509TrustManager();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(interceptor);
        builder.connectTimeout(30, TimeUnit.SECONDS);
        builder.readTimeout(25, TimeUnit.SECONDS);
        builder.sslSocketFactory(SSLFactoryUtil.getSocketFactory(trustManager), trustManager);
        builder.hostnameVerifier(SSLFactoryUtil.getHostnameVerifier());//忽略校验
        return builder.build();
    }

    @Bean
    public Retrofits retrofits() {
        return new Retrofits();
    }

}
